package me.firstapp.repository.bbs.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.bbs.Member;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.bbs.MemberRepository;

@Repository
public class MemberRepositoryImpl extends EntityRepositoryImpl<Member> implements MemberRepository {
	@Override
	protected Class<Member> getPersistentClass() {
		return Member.class;
	}

	public Member findMemberByName(String name, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM Member bean WHERE 1=1 ");

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		sql.append(" AND bean.name=:name ");
		parameters.put("name", name);

		List<Member> results = super.query(Member.class, sql.toString(), parameters);
		return results.isEmpty() ? null : results.get(0);
	}

	public Member findMemberByEmail(String email, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM Member bean WHERE 1=1 ");

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		sql.append(" AND bean.email=:email ");
		parameters.put("email", email);

		List<Member> results = super.query(Member.class, sql.toString(), parameters);
		return results.isEmpty() ? null : results.get(0);
	}

	public Member findMemberByMobile(String mobile, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM Member bean WHERE 1=1 ");

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		sql.append(" AND bean.mobile=:mobile ");
		parameters.put("mobile", mobile);

		List<Member> results = super.query(Member.class, sql.toString(), parameters);
		return results.isEmpty() ? null : results.get(0);
	}

	@Override
	public Page<Member> findMemberPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType, Date startTime, Date endTime, Long apiClientId) {
		StringBuilder sql = new StringBuilder(" FROM Member bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<String, Object>();

		if (searchParams != null) {
			Set<String> keys = searchParams.keySet();
			for (String key : keys) {
				Object value = searchParams.get(key);
				if (value != null) {
					sql.append(" AND bean." + key + " LIKE :value ");
					parameters.put("value", "%" + value + "%");
				}
			}
		}
		Date _startTime = null;
		if (startTime != null) {
			String startTimeStr = DateUtils.getDateStr(startTime, "yyyy-MM-dd") + " 00:00:00";
			_startTime = DateUtils.getDateFromDateStr(startTimeStr, "yyyy-MM-dd HH:mm:ss");

		}
		Date _endTime = null;
		if (endTime != null) {
			String endTimeStr = DateUtils.getDateStr(endTime, "yyyy-MM-dd") + " 23:59:59";
			_endTime = DateUtils.getDateFromDateStr(endTimeStr, "yyyy-MM-dd HH:mm:ss");
		}

		if (_startTime != null) {
			sql.append(" AND bean.registerTime >=:startTime ");
			parameters.put("startTime", _startTime);
		}
		if (_endTime != null) {
			sql.append(" AND bean.registerTime <=:endTime");
			parameters.put("endTime", _endTime);
		}
		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);

		if (StrUtils.isNotNULL(sortParam) && StrUtils.isNotNULL(sortType)) {
			sql.append(" ORDER BY " + sortParam + " " + sortType);
		}

		return super.queryForPage(Member.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	public Member findMemberById(Long id, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM Member bean WHERE 1=1 ");

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);

		sql.append(" AND bean.id=:id ");
		parameters.put("id", id);

		List<Member> results = super.query(Member.class, sql.toString(), parameters);
		return results.isEmpty() ? null : results.get(0);
	}

	@Override
	public Long findMemberCountsByRegisterTime(Date startTime, Date endTime, Long apiClientId) {
		StringBuilder sql = new StringBuilder("SELECT COUNT(bean.id) FROM Member bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		Date _startTime = null;
		if (startTime != null) {
			String startTimeStr = DateUtils.getDateStr(startTime, "yyyy-MM-dd") + " 00:00:00";
			_startTime = DateUtils.getDateFromDateStr(startTimeStr, "yyyy-MM-dd HH:mm:ss");

		}
		Date _endTime = null;
		if (endTime != null) {
			String endTimeStr = DateUtils.getDateStr(endTime, "yyyy-MM-dd") + " 23:59:59";
			_endTime = DateUtils.getDateFromDateStr(endTimeStr, "yyyy-MM-dd HH:mm:ss");
		}

		if (startTime != null && endTime != null) {
			sql.append(" AND bean.registerTime >=:startTime ");
			parameters.put("startTime", _startTime);
			sql.append(" AND bean.registerTime <=:endTime ");
			parameters.put("endTime", _endTime);

		} else if (startTime != null && endTime == null) {
			sql.append(" AND bean.registerTime >=:startTime ");
			parameters.put("startTime", _startTime);
		} else if (startTime == null && endTime != null) {
			sql.append(" AND bean.registerTime <=:endTime ");
			parameters.put("endTime", _endTime);
		}

		List<Long> result = super.query(Long.class, sql.toString(), parameters);
		return result.isEmpty() ? 0 : result.get(0);
	}

}
